package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.FindPeopleEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.WorkYear;
import com.sgb.kjwl.model.entity.WorkerLevel;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@author liwei
*@date 2020/12/19
*description:
*/
public class PackersFindPeopleModel extends AndroidViewModel {


    private PickSelectEntity all;

    private int page = 1;
    private int limit = 10;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<FindPeopleEntity>> workerList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> workLevel = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> typeList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> workYearList = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> cityList = new MutableLiveData<>();

    public PackersFindPeopleModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getNewList(String industryNo, String categoryNo, String workType, String workLevel, String type, String provinceNo, String cityNo, String areaNo, boolean b,String time) {
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        if (!TextUtils.isEmpty(industryNo)) {
            map.put("industryNo", industryNo);
        }
        if (!TextUtils.isEmpty(categoryNo)) {
            map.put("categoryNo", categoryNo);
        }
        if (!TextUtils.isEmpty(workType)) {
            map.put("workType", workType);
        }
        if (!TextUtils.isEmpty(workLevel)) {
            map.put("proficiency", workLevel);
        }
        if (TextUtils.isEmpty(type)) {
            map.put("workYear", "0");
        } else {
            map.put("workYear", type);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNoList", stringList);
        }
        if (!TextUtils.isEmpty(time)) {
            map.put("publishTime", time);
        }
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPackerPeopleList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<FindPeopleEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<FindPeopleEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                workerList.postValue(data.getData().getList());
            }
        });
    }


    public void getWorkerLevel() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getWorkerLevel().enqueue(new ICallBack<BaseEntity<List<WorkerLevel>>>() {
                    @Override
                    public void onSuccess(BaseEntity<List<WorkerLevel>> data1) {
                        DialogHelper.dismissProgressDialog();
                        List<WorkerLevel> workYears = data1.getData();
                        List<PickSelectEntity> data = new ArrayList<>();
                        if (workYears != null && workYears.size() > 0) {
                            for (WorkerLevel workerLevel : workYears) {
                                PickSelectEntity entity = new PickSelectEntity();
                                entity.setNo(String.valueOf(workerLevel.getType()));
                                entity.setProjectscale(workerLevel.getDesc());
                                data.add(entity);
                            }
                        }
                        data.add(0, all);
                        workLevel.postValue(data);
                    }
                });
    }


    public void getLinkAgeWorkType() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLinkAgeWorkType().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data1) {
                DialogHelper.dismissProgressDialog();
                List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data = new ArrayList<>();
                data.add(0,new PickFilterEntity("","全部",""));
                for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data1.getData()) {
                    List<PickFilterEntity<List<PickFilterEntity>>> dataSecond = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : datum.getChildren()) {
                        List<PickFilterEntity> dataThird = new ArrayList<>();
                        for (AddressEntity childChild : child.getChildren()) {
                            dataThird.add(new PickFilterEntity(childChild.getNo(),childChild.getName()));
                        }
                        dataSecond.add(new PickFilterEntity<>(child.getNo(),child.getName(),dataThird));
                    }
                    data.add(new PickFilterEntity<>(datum.getNo(),datum.getName(),dataSecond));
                }
                typeList.postValue(data);
            }
        });
    }


    public void getWorkYear() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getWorkYearList().enqueue(new ICallBack<BaseEntity<List<WorkYear>>>() {
            @Override
            public void onSuccess(BaseEntity<List<WorkYear>> data1) {
                DialogHelper.dismissProgressDialog();
                List<WorkYear> workYears = data1.getData();
                List<PickSelectEntity> data = new ArrayList<>();
                if (workYears != null && workYears.size() > 0) {
                    for (WorkYear workYear : workYears) {
                        PickSelectEntity entity = new PickSelectEntity();
                        entity.setNo(String.valueOf(workYear.getType()));
                        entity.setProjectscale(workYear.getDesc());
                        data.add(entity);
                    }
                }
                workYearList.postValue(data);
            }
        });
    }


    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                cityList.postValue(data.getData());
            }
        });
    }

}
